package com.aaron.platform.dao;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页类
 * @author chenzhuohua
 *
 * @param <T> class type
 */
public class PageInfo<T>{

    private int pageSize;
    private int startIndex;
    private int totalCount;
    private List<T> items;

    public final void init() {
        pageSize = 10;
        startIndex = 0;
        totalCount = 0;
        items = new ArrayList<T>();
    }

    public PageInfo(List items, int pageSize, int startIndex, int totalCount) {
        this.items = items;
        this.pageSize = pageSize;
        this.startIndex = startIndex;
        this.totalCount = totalCount;
    }

    public PageInfo(List items, int pageSize, int totalCount) {
        this.items = items;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }

    public PageInfo(List items, int totalCount) {
        this.items = items;
        this.totalCount = totalCount;
    }

    public PageInfo() {
        init();
    }

    public List<T> getItems() {
        if (items == null) {
            items = new ArrayList<T>();
        }
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        if (totalCount <= 0) {
            startIndex = 0;
        } 
        this.startIndex = startIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getDisplayCount() {
        return items.size();
    }

    public void setTotalCount(int totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
        } else {
            this.totalCount = 0;
        }
    }

    public int getTotalPages() {
        int pages = totalCount / pageSize;
        if (totalCount != 0 && totalCount % pageSize != 0) {
            pages++;
        }
        return pages;
    }

    public int getCurrentPage() {
        int currentPage = startIndex / pageSize;
        if ((totalCount != 0 && currentPage == 0) || startIndex % pageSize != 0) {
            currentPage++;
        }
        return currentPage;
    }

    public int getNextIndex() {
        int nextIndex = startIndex + pageSize;
        if (nextIndex < totalCount) {
            startIndex = startIndex + pageSize;
        } 
        return startIndex;
    }

    public int getPreviousIndex() {
        int preIndex = startIndex - pageSize;
        if (preIndex < 0) {
            startIndex = 0;
        } else {
            startIndex = startIndex - pageSize;
        }
        return startIndex;
    }

    public int getfirstIndex() {
        startIndex = 0;
        return startIndex;
    }

    public int getLastIndex() {
        int pages = totalCount / pageSize;
        if (totalCount == 0 || totalCount % pageSize != 0) {
            startIndex = pages * pageSize;
        } else {
            pages--;
            startIndex = pages * pageSize;
        }
        return startIndex;
    }
}
